package com.hgd.evp.admin.rest;

import com.hgd.evp.admin.biz.EMenuBiz;
import com.hgd.evp.admin.entity.EMenu;
import com.hgd.evp.admin.vo.MenuTree;
import com.hgd.evp.common.utils.ObjectResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("baseMenu")
@Api(tags = "菜单管理")
@Log4j
public class EMenuController {
    @Autowired
    private EMenuBiz eMenuBiz;

    @PostMapping("/instance")
    @ApiOperation("添加菜单")
    public ObjectResultResponse<Void> addEMenu(@RequestBody @ApiParam(value = "待添加对象") EMenu eMenu, BindingResult bindingResult) {
        log.info("/baseMenu/instance==>添加菜单");
        ObjectResultResponse<Void> oResult = new ObjectResultResponse<>();
        if (bindingResult.hasErrors()) {
            oResult.setStatus(400);
            oResult.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return oResult;
        }

        oResult = eMenuBiz.addEmenu(eMenu);
        return oResult;
    }

    @GetMapping("/tree")
    @ApiOperation("获取菜单树")
    public List<MenuTree> eMenuTree(){
        log.info("/baseMenu/tree==>获取菜单树");
        return eMenuBiz.getMenuTree();
    }
}